package com.hongye.house.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.hongye.house.config.HongYeESConfig;
import com.hongye.house.entity.SaleHomeEntity;
import org.apache.lucene.search.TotalHits;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hongye.common.utils.PageUtils;
import com.hongye.common.utils.Query;

import com.hongye.house.dao.LeaseHomeDao;
import com.hongye.house.entity.LeaseHomeEntity;
import com.hongye.house.service.LeaseHomeService;


@Service("leaseHomeService")
public class LeaseHomeServiceImpl extends ServiceImpl<LeaseHomeDao, LeaseHomeEntity> implements LeaseHomeService {


    @Autowired
    private RestHighLevelClient client;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        //默认按发布时间倒序
        QueryWrapper<LeaseHomeEntity> homeEntityQueryWrapper = new QueryWrapper<LeaseHomeEntity>().orderByDesc("house_createtime");
        IPage<LeaseHomeEntity> page = this.page(
                new Query<LeaseHomeEntity>().getPage(params),
                homeEntityQueryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public List<LeaseHomeEntity> rentinglist(Integer houseagent) {
        return baseMapper.selectList(new QueryWrapper<LeaseHomeEntity>().eq("house_agent",houseagent));
    }

    @Override
    public void esinsert() {
        List<LeaseHomeEntity> list = baseMapper.selectList(null);
        //批量请求
        BulkRequest request = new BulkRequest();
        list.stream().forEach( a ->{
            //创建索引
            IndexRequest indexRequest = new IndexRequest("leasehouseshop").id(a.getHouseId().toString());
            //因为我的a是个对象 循环查每一条数据 批量添加
            //es里添加规定的要string类型所以要把a 转成string类型进行操作
            String s = JSONObject.toJSONString(a);

            IndexRequest source = indexRequest.source(s, XContentType.JSON);
            //添加
            request.add(indexRequest,source);
        });
        try {
            client.bulk(request, HongYeESConfig.COMMON_OPTIONS);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public Integer numberofrentals() {
        return baseMapper.selectCount(new QueryWrapper<LeaseHomeEntity>());
    }

    @Override
    public List<LeaseHomeEntity> esleasesearch(Integer housePrice) throws IOException {
        SearchRequest searchRequest = new SearchRequest("leasehouseshop");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
        if (housePrice != null) {
            boolQueryBuilder.must(QueryBuilders.termQuery("housePrice", housePrice));
        }
        searchSourceBuilder.query(boolQueryBuilder);
        searchRequest.source(searchSourceBuilder);

        SearchResponse searchResponse = client.search(searchRequest, HongYeESConfig.COMMON_OPTIONS);
        SearchHits hits = searchResponse.getHits();
        TotalHits totalHits = hits.getTotalHits();
// the total number of hits, must be interpreted in the context of totalHits.relation
        Long numHits = totalHits.value;
        SearchHit[] searchHits = hits.getHits();
        List<LeaseHomeEntity> list1 = Arrays.stream(searchHits).map(b -> {
            LeaseHomeEntity leaseHomeEntity = JSONObject.parseObject(b.getSourceAsString(), LeaseHomeEntity.class);
            return leaseHomeEntity;
        }).collect(Collectors.toList());

        return list1;

    }

}